package com.rdgzs.framework.web;


import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.springframework.dao.DataAccessException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.rdgzs.framework.service.ServiceException;

@SuppressWarnings("serial")

public class ExceptionInterceptor extends AbstractInterceptor {
    @SuppressWarnings("unchecked")
    public String intercept(ActionInvocation actionInvocation) throws Exception {
       String result = "";
       //ToDo
       //如果是业务异常，那么我就message就使用ex的。不是我们自定义的。
       //对于form提交的异常。不封装成ajaxException，封装成formException，通过配置struts2文件使用pagejsp的处理方式。
		try {
			result = actionInvocation.invoke();
		} catch (DataAccessException ex) {
			String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.DataAccessException, null);
			throw new AjaxException(exceptionMessage, ex);
		} catch (NullPointerException ex) {
			String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.NullPointerException, null);
			throw new AjaxException(exceptionMessage, ex);
		} catch (IOException ex) {
			String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.IOException, null);
			throw new AjaxException(exceptionMessage, ex);
       } catch (ClassNotFoundException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.ClassNotFoundException, null);
    	   throw new AjaxException(exceptionMessage, ex);
       } catch (ArithmeticException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.ArithmeticException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (ArrayIndexOutOfBoundsException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.ArrayIndexOutOfBoundsException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (IllegalArgumentException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.IllegalArgumentException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (ClassCastException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.ClassCastException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (SecurityException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.SecurityException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (SQLException ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.SQLException, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (NoSuchMethodError ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.NoSuchMethodError, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (InternalError ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.InternalError, null);
		   throw new AjaxException(exceptionMessage, ex);
       } catch (ServiceException ex) {// 业务层的错误异常
    	   //业务消息
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ex.getErrorCode(), null);
           throw new AjaxException(exceptionMessage,ex);
       } catch (Exception ex) {
    	   String exceptionMessage = ExceptionInterceptor.getFormattedMessage(ExceptionMessage.Exception, null);
		   throw new AjaxException(exceptionMessage, ex);
       }
       return result;
    }
    public static String getFormattedMessage(int i, Object aobj[]) {
		String s;
		try {
			s = bundle.getString(String.valueOf(i));
			if (aobj != null)
				s = MessageFormat.format(s, aobj);
		} catch (MissingResourceException missingresourceexception) {
			s = missingresourceexception.getMessage();
		}
		return s;
	}
	private static ResourceBundle bundle = ResourceBundle.getBundle("exceptionMessages", Locale.getDefault());

}


